<?php

namespace Wechat\Controller;
use Think\Controller;

vendor('wxpay.WxPay#Api');
vendor('wxpay.WxPay#Notify');

class NotifyvipController extends Controller {


    public function _initialize()
    {
        header("Content-Type:text/html; charset=utf-8");
    }

    public function wxpay_notify() {
        $notify = new PayNotifyCallBack();
        $notify->Handle(false);
    }

}


class PayNotifyCallBack extends \WxPayNotify
{
    //查询订单
    public function Queryorder($transaction_id)
    {
        $input = new \WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = \WxPayApi::orderQuery($input, 30);
        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS")
        {
            return true;
        }
        return false;
    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg)
    {
        //trace(json_encode($data),'','DEBUG',true);
        $notfiyOutput = array();
        if (!array_key_exists("transaction_id", $data)) {
            $msg = "输入参数不正确";
            trace($msg, '', 'DEBUG', true);
            return false;
        }
        //查询订单，判断订单真实性
        if (!$this->Queryorder($data["transaction_id"])) {
            $msg = "订单查询失败";
            trace($msg, '', 'DEBUG', true);
            return false;
        }

        $order = M('MemberOrder')->where(array('order_sn' => $data['out_trade_no']))->find();
        if (empty($order)) {
            $msg = "订单查询失败";
            return false;
        }
        if ($data['total_fee'] != ($order['pay_amount']*100)){
            $msg = "订单金额不符";
            return false;
        }
        if ($order['pay_state'] != 0) {
            $msg = "订单查询失败";
            return false;
        }

        $model = D();
        try {
            $model->startTrans();
            $orderArr = array();
            $orderArr['pay_time'] = strtotime($data['time_end']);
            $orderArr['pay_sn'] = $data['transaction_id'];
            $orderArr['update_time'] = time();
            $orderArr['pay_state'] = 1;
            $result = M('MemberOrder')->where(array('order_id' => $order['order_id']))->save($orderArr);
            $endTime = date('Y') + 1 . '-' . date('m-d');
            $vip = array('vip' => 1, 'vip_end_time' => $endTime, 'vip_number' => getVipSN($order['member_id']), 'update_time' => time());
            $re = M('Member')->where(array('member_id' => $order['member_id']))->save($vip);

            $pay = array(
                'order_id' => $order['order_id'],
                'order_sn' => $order['order_sn'],
                'store_id' => 0,
                'member_id' => $order['member_id'],
                'pay_id' => $order['pay_id'],
                'pay_code' => $order['pay_code'],
                'pay_time' => $orderArr['pay_time'],
                'pay_amount' => $order['pay_amount'],
                'pay_state' => 1,
                'pay_sn' => $data['transaction_id'],
                'notify_id' => $data['mch_id'],
                'notify_time' => $orderArr['pay_time'],
                'notify_desc' => json_encode($data),
                'create_time' => time(),
            );
            $result = M('OrderPay')->add($pay);
            $model->commit();
        } catch (\Exception $e) {
            $msg = "订单查询失败";
            $model->rollback();
            return false;
        }
        return true;
    }
}